package pl.edu.agh.portal.application.portlets;

import java.io.IOException;
import java.util.ArrayList;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletConfig;
import javax.portlet.PortletException;
import javax.portlet.PortletMode;
import javax.portlet.PortletRequestDispatcher;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

import pl.edu.agh.portal.application.utils.PortletsUtils;
import pl.edu.agh.thesis.webservices.employee.EmployeeManagerSkeleton;
import pl.edu.agh.thesis.webservices.employee.client.EmployeeManagerStub;
import pl.edu.agh.thesis.webservices.employee.client.EmployeeUtils;
import pl.edu.agh.thesis.webservices.project.ProjectManagerSkeleton;
import pl.edu.agh.thesis.webservices.project.client.ProjectManagerStub;
import pl.edu.agh.thesis.webservices.project.client.ProjectUtils;
import pl.edu.agh.thesis.webservices.task.TaskManagerSkeleton;
import pl.edu.agh.thesis.webservices.task.client.TaskManagerStub;
import pl.edu.agh.thesis.webservices.task.client.TaskUtils;

public class TaskListPortlet extends ThesisGenericPortlet {

	private static final String NORMAL_VIEW = "/task_list_portlet.jsp";
	
	private PortletRequestDispatcher normalView;
	
	public void doView(RenderRequest request, RenderResponse response) throws PortletException, IOException {
		
		int employeeId = EmployeeUtils.validate(PortletsUtils.temp_session);
		/*
		TaskManagerStub.Task[] tasksAssignee = TaskUtils.getAll(PortletsUtils.temp_session, String.valueOf(employeeId), TaskManagerSkeleton.USER_ID_RESTRICTION_ASSIGNEE);
		request.setAttribute(PortletsUtils.ATTRIBUTE_TASK_LIST_ASSIGNEE, tasksAssignee);
		
		TaskManagerStub.Task[] tasksReporter = TaskUtils.getAll(PortletsUtils.temp_session, String.valueOf(employeeId), TaskManagerSkeleton.USER_ID_RESTRICTION_REPORTER);
		request.setAttribute(PortletsUtils.ATTRIBUTE_TASK_LIST_REPORTER, tasksReporter);
		*/
		TaskManagerStub.Task[] tasksAssignee = getAllTaskForAssignee(employeeId);
		request.setAttribute(PortletsUtils.ATTRIBUTE_TASK_LIST_ASSIGNEE, tasksAssignee);
		
		TaskManagerStub.Task[] tasksReporter = getAllTaskForReporter(employeeId);
		request.setAttribute(PortletsUtils.ATTRIBUTE_TASK_LIST_REPORTER, tasksReporter);
		
		normalView.include(request, response);
	}
	
	@Override
	public void processAction(ActionRequest request, ActionResponse response) throws PortletException, IOException {
		String remove = request.getParameter(PortletsUtils.BUTTON_REMOVE);
		String details = request.getParameter(PortletsUtils.BUTTON_DETAILS);
				
		if (remove != null) {
			response.sendRedirect("/portal/portal/task_remove.psml");
			PortletsUtils.vault.put("taskId", remove);
		} else if (details != null) {			
			response.sendRedirect("/portal/portal/task_details.psml");
			PortletsUtils.vault.put("taskId", details);
		}		
		response.setPortletMode(PortletMode.VIEW);		
	}	
	
	public void init(PortletConfig config) throws PortletException {
		super.init(config);
		normalView = config.getPortletContext().getRequestDispatcher(NORMAL_VIEW);
	}
	
	public void destroy() {
		normalView = null;
		super.destroy();
	}
	
	private TaskManagerStub.Task[] getAllTaskForAssignee(int employeeId) {
		ArrayList<TaskManagerStub.Task> result = new ArrayList<TaskManagerStub.Task>();
		EmployeeManagerStub.Employee[] allEmployees = EmployeeUtils.getAll(PortletsUtils.temp_session, EmployeeManagerSkeleton.GET_ALL_CRITERIA, ""); 
		if (allEmployees != null) {
			for (EmployeeManagerStub.Employee employee : allEmployees) {
				ProjectManagerStub.Project[] allProjects = ProjectUtils.getAll(PortletsUtils.temp_session, String.valueOf(employee.getId()), ProjectManagerSkeleton.USER_ID_RESTRICTION);
				if (allProjects != null) {
					for (ProjectManagerStub.Project project : allProjects) {
						TaskManagerStub.Task[] allTasks = TaskUtils.getAll(PortletsUtils.temp_session, employee.getId() + " " + project.getId(), TaskManagerSkeleton.PROJECT_ID_RESTRICTION);
						if (allTasks != null) {
							for (TaskManagerStub.Task task : allTasks) {
								if (task.getAssignee() == employeeId) {
									result.add(task);
								}
							}
						}
					}
				}
			}			
			
		}
		if (result.size() > 0){
			return result.toArray(new TaskManagerStub.Task[0]);
		}
		return null;
	}
	
	private TaskManagerStub.Task[] getAllTaskForReporter(int employeeId) {
		ArrayList<TaskManagerStub.Task> result = new ArrayList<TaskManagerStub.Task>();
		EmployeeManagerStub.Employee[] allEmployees = EmployeeUtils.getAll(PortletsUtils.temp_session, EmployeeManagerSkeleton.GET_ALL_CRITERIA, ""); 
		if (allEmployees != null) {
			for (EmployeeManagerStub.Employee employee : allEmployees) {
				ProjectManagerStub.Project[] allProjects = ProjectUtils.getAll(PortletsUtils.temp_session, String.valueOf(employee.getId()), ProjectManagerSkeleton.USER_ID_RESTRICTION);
				if (allProjects != null) {
					for (ProjectManagerStub.Project project : allProjects) {
						TaskManagerStub.Task[] allTasks = TaskUtils.getAll(PortletsUtils.temp_session, employee.getId() + " " + project.getId(), TaskManagerSkeleton.PROJECT_ID_RESTRICTION);
						if (allTasks != null) {
							for (TaskManagerStub.Task task : allTasks) {
								if (task.getReporter() == employeeId) {
									result.add(task);
								}
							}
						}
					}
				}
			}			
			
		}
		if (result.size() > 0){
			return result.toArray(new TaskManagerStub.Task[0]);
		}
		return null;
	}
			
}
